MODULE_NAME='IP Reporter'
(
	Dev vdvConnection,
	Integer nClientID
)
(***********************************************************)
(***********************************************************)
(*  FILE_LAST_MODIFIED_ON: 04/04/2006  AT: 11:33:16        *)
(***********************************************************)
(* System Type : NetLinx                                   *)
(***********************************************************)
(* REV HISTORY:                                            *)
(***********************************************************)
(*
    $History: $
*)    
//#DEFINE DEBUG = 1

(***********************************************************)
(*          DEVICE NUMBER DEFINITIONS GO BELOW             *)
(***********************************************************)
DEFINE_DEVICE

(***********************************************************)
(*               CONSTANT DEFINITIONS GO BELOW             *)
(***********************************************************)
DEFINE_CONSTANT
Char strIPAddress[] = 'www.paragonintranet.com'

cnTimelinePollID = 32
#IF_DEFINED DEBUG
Long cnTimelineTimes[] = {20000}
#ELSE
Long cnTimelineTimes[] = {86400000}
#END_IF
cnTimelineQty = 1

(***********************************************************)
(*              DATA TYPE DEFINITIONS GO BELOW             *)
(***********************************************************)
DEFINE_TYPE

(***********************************************************)
(*               VARIABLE DEFINITIONS GO BELOW             *)
(***********************************************************)
DEFINE_VARIABLE
Volatile SInteger nResult

(***********************************************************)
(*               LATCHING DEFINITIONS GO BELOW             *)
(***********************************************************)
DEFINE_LATCHING

(***********************************************************)
(*       MUTUALLY EXCLUSIVE DEFINITIONS GO BELOW           *)
(***********************************************************)
DEFINE_MUTUALLY_EXCLUSIVE

(***********************************************************)
(*        SUBROUTINE/FUNCTION DEFINITIONS GO BELOW         *)
(***********************************************************)
(* EXAMPLE: DEFINE_FUNCTION <RETURN_TYPE> <NAME> (<PARAMETERS>) *)
(* EXAMPLE: DEFINE_CALL '<NAME>' (<PARAMETERS>) *)

Define_Call 'DEBUG_OUTPUT' (Integer nLine, Char strData[])
{
	#IF_DEFINED DEBUG
	Send_String 0,"__FILE__,' Line:',Itoa(nLine),' ',strData"
	#END_IF
}

DEFINE_FUNCTION CHAR[100] GET_IP_ERROR (LONG lERR)
{
  SELECT
  {
    ACTIVE (lERR = 0):
      RETURN "";
    ACTIVE (lERR = 2):
      RETURN "'IP ERROR (',ITOA(lERR),'): General Failure (IP_CLIENT_OPEN/IP_SERVER_OPEN)'";
    ACTIVE (lERR = 4):
      RETURN "'IP ERROR (',ITOA(lERR),'): unknown host (IP_CLIENT_OPEN)'";
    ACTIVE (lERR = 6):
      RETURN "'IP ERROR (',ITOA(lERR),'): connection refused (IP_CLIENT_OPEN)'";
    ACTIVE (lERR = 7):
      RETURN "'IP ERROR (',ITOA(lERR),'): connection timed out (IP_CLIENT_OPEN)'";
    ACTIVE (lERR = 8):
      RETURN "'IP ERROR (',ITOA(lERR),'): unknown connection error (IP_CLIENT_OPEN)'";
    ACTIVE (lERR = 14):
      RETURN "'IP ERROR (',ITOA(lERR),'): local port already used (IP_CLIENT_OPEN/IP_SERVER_OPEN)'";
    ACTIVE (lERR = 16):
      RETURN "'IP ERROR (',ITOA(lERR),'): too many open sockets (IP_CLIENT_OPEN/IP_SERVER_OPEN)'";
    ACTIVE (lERR = 10):
      RETURN "'IP ERROR (',ITOA(lERR),'): Binding error (IP_SERVER_OPEN)'";
    ACTIVE (lERR = 11):
      RETURN "'IP ERROR (',ITOA(lERR),'): Listening error (IP_SERVER_OPEN)'";
    ACTIVE (lERR = 15):
      RETURN "'IP ERROR (',ITOA(lERR),'): UDP socket already listening (IP_SERVER_OPEN)'";
    ACTIVE (lERR = 9):
      RETURN "'IP ERROR (',ITOA(lERR),'): Already closed (IP_CLIENT_CLOSE/IP_SERVER_CLOSE)'";
    ACTIVE (1):
      RETURN "'IP ERROR (',ITOA(lERR),'): Unknown'";
  }
}

(***********************************************************)
(*                STARTUP CODE GOES BELOW                  *)
(***********************************************************)
DEFINE_START
Wait 300
{
	nResult = 1
	nResult = Ip_Client_Open (vdvConnection.Port, strIPAddress, 80, 1)
	Timeline_Create(cnTimelinePollID, cnTimelineTimes, cnTimelineQty, Timeline_Relative, Timeline_Repeat)
}

(***********************************************************)
(*                THE EVENTS GO BELOW                      *)
(***********************************************************)
DEFINE_EVENT

Timeline_Event[cnTimelinePollID]
{
	nResult = Ip_Client_Open (vdvConnection.Port, strIPAddress, 80, 1)
}

Data_Event[vdvConnection]
{
	OnError:
	{
    If (Data.Number != 14)
      nResult = 1  // set connection state to a invalid number (0 = connected)
    Call 'DEBUG_OUTPUT' (__LINE__,"GET_IP_ERROR (Data.Number)")
	}
	Online:
	{
		Send_String vdvConnection,"'GET /exec/ip/?clientID=',Itoa(nClientID),' HTTP/1.1',13,10,'Host:www.paragonintranet.com',13,10,13,10"
		Call 'DEBUG_OUTPUT' (__LINE__,"'GET /exec/ip/?clientID=',Itoa(nClientID),' HTTP/1.1',13,10,'Host:www.paragonintranet.com',13,10,13,10")
		//Ip_Client_Close(vdvConnection.Port)
	}
	String:
	{
		Call 'DEBUG_OUTPUT' (__LINE__,"'Data.Text:',Data.Text")
	}
}

(***********************************************************)
(*            THE ACTUAL PROGRAM GOES BELOW                *)
(***********************************************************)
DEFINE_PROGRAM

(***********************************************************)
(*                     END OF PROGRAM                      *)
(*        DO NOT PUT ANY CODE BELOW THIS COMMENT           *)
(***********************************************************)
